/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewUser.java
 *
 * Created on 24-mei-2009, 20:25:27
 */

package Gui.login;

import database.Database;
import database.dateTime.DBDateTime;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import main.Main;

/**
 *
 * @author LESLEY
 */
public class NewUser extends javax.swing.JFrame {

    /** Creates new form NewUser */
    public NewUser() {
    	
        initComponents();
        
        pack();
        setLocationRelativeTo(null);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    private final void createVCard() {
        String name = nameTextField.getText();
        String fname = forenameTextField.getText();
        File vcard = new File(name + fname + ".vcf");
        if (!vcard.exists()) {
            try {
                int i = 1;
                while (!vcard.createNewFile()) {
                     vcard = new File(name + fname + "(" + i +").vcf");
                     i++;
                }
                FileOutputStream fos;
                DataOutputStream dos;
                fos = new FileOutputStream(vcard);
                dos=new DataOutputStream(fos);
                String email = emailTextField.getText();
                dos.writeChars("BEGIN:VCARD \n VERSION:3.0 \n N:" +
                        name + ";" + fname + " \n " +
                        "FN:" + fname + " " + name + " \n " +
                        "EMAIL;TYPE=PREF,INTERNET:" + email + " \n " +
                        "REV:" + (new DBDateTime()).toString() + " \n " +
                        "END:VCARD");
            } catch (Exception e) {
                // TODO remove
                e.printStackTrace();
                return;
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginPanel = new javax.swing.JPanel();
        loginUsernameLabel = new javax.swing.JLabel();
        loginPasswordLabel1 = new javax.swing.JLabel();
        loginPassword2Label = new javax.swing.JLabel();
        loginUsernameTextField = new javax.swing.JTextField();
        loginPasswordField1 = new javax.swing.JPasswordField();
        loginPasswordField2 = new javax.swing.JPasswordField();
        starLabel1 = new javax.swing.JLabel();
        starLabel2 = new javax.swing.JLabel();
        starLabel3 = new javax.swing.JLabel();
        otherDataPanel = new javax.swing.JPanel();
        forenameLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        forenameTextField = new javax.swing.JTextField();
        nameTextField = new javax.swing.JTextField();
        emailTextField = new javax.swing.JTextField();
        phonenumberLabel = new javax.swing.JLabel();
        phonenumberTextField = new javax.swing.JTextField();
        moreInfoLabel = new javax.swing.JLabel();
        starLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        moreInfoTextArea = new javax.swing.JTextArea();
        necessaryStarsLabel = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("USIM Pro - Nieuwe gebruiker");
        setResizable(false);

        loginPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Login gegevens", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        loginUsernameLabel.setText("Gebruikersnaam: ");

        loginPasswordLabel1.setText("Wachtwoord: ");

        loginPassword2Label.setText("Bevestig wachtwoord: ");

        starLabel1.setText("*");

        starLabel2.setText("*");

        starLabel3.setText("*");

        javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(loginPanelLayout);
        loginPanelLayout.setHorizontalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(loginUsernameLabel)
                    .addComponent(loginPasswordLabel1)
                    .addComponent(loginPassword2Label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(loginPasswordField1)
                    .addComponent(loginUsernameTextField)
                    .addComponent(loginPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(starLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(starLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(starLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        loginPanelLayout.setVerticalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginUsernameLabel)
                    .addComponent(loginUsernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(starLabel1))
                .addGap(4, 4, 4)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginPasswordLabel1)
                    .addComponent(loginPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(starLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginPassword2Label)
                    .addComponent(loginPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(starLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        otherDataPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Overige gegevens", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
        otherDataPanel.setPreferredSize(new java.awt.Dimension(391, 354));

        forenameLabel.setText("Voornaam: ");

        nameLabel.setText("Naam: ");

        emailLabel.setText("Email: ");

        phonenumberLabel.setText("Telefoonnummer: ");

        moreInfoLabel.setText("Meer informatie: ");

        starLabel4.setText("*");

        moreInfoTextArea.setColumns(20);
        moreInfoTextArea.setLineWrap(true);
        moreInfoTextArea.setRows(7);
        moreInfoTextArea.setTabSize(4);
        moreInfoTextArea.setMargin(new java.awt.Insets(5, 5, 5, 5));
        jScrollPane1.setViewportView(moreInfoTextArea);

        necessaryStarsLabel.setText("(* verplichte velden)");

        cancelButton.setText("Annuleren");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout otherDataPanelLayout = new javax.swing.GroupLayout(otherDataPanel);
        otherDataPanel.setLayout(otherDataPanelLayout);
        otherDataPanelLayout.setHorizontalGroup(
            otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(otherDataPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(otherDataPanelLayout.createSequentialGroup()
                        .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(forenameLabel)
                            .addComponent(nameLabel)
                            .addComponent(emailLabel))
                        .addGap(36, 36, 36)
                        .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(emailTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                            .addComponent(nameTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                            .addComponent(forenameTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(starLabel4))
                    .addGroup(otherDataPanelLayout.createSequentialGroup()
                        .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(otherDataPanelLayout.createSequentialGroup()
                                .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(phonenumberLabel)
                                    .addComponent(moreInfoLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(phonenumberTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)))
                            .addGroup(otherDataPanelLayout.createSequentialGroup()
                                .addComponent(necessaryStarsLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE)
                                .addComponent(okButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cancelButton)))
                        .addGap(10, 10, 10)))
                .addGap(11, 11, 11))
        );
        otherDataPanelLayout.setVerticalGroup(
            otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(otherDataPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(forenameLabel)
                    .addComponent(forenameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailLabel)
                    .addComponent(emailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(starLabel4))
                .addGap(18, 18, 18)
                .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phonenumberTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(phonenumberLabel))
                .addGap(18, 18, 18)
                .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(moreInfoLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(otherDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(necessaryStarsLabel)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(otherDataPanel, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(loginPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(otherDataPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

        if (loginPasswordField1.getText() != loginPasswordField2.getText()) {
            // TODO popup window -> fout in wachtwoord
            loginPasswordField1.setText("");
            loginPasswordField2.setText("");
            return;
        }
        if (Main.save) {
            createVCard();
        }
        try {
            String newDBName = nameTextField.getText() + forenameTextField.getText();
            Main.db = new Database(newDBName);
            int i = 1;
            while (!Main.db.createDBFile(false)) {
                newDBName = nameTextField.getText() + forenameTextField.getText() + 
                        "(" + i + ")";
                Main.db = new Database(newDBName);
                i++;
            }
            Main.initConnection(newDBName, loginUsernameTextField.getText(),
                    loginPasswordField1.getText());
            Main.userDB.add(loginUsernameTextField.getText(), 
                    loginPasswordField1.getText(), newDBName);
            if (!Main.admin) {
                Main.db.stud.add(emailTextField.getText(), nameTextField.getText(), 
                        forenameTextField.getText(), phonenumberTextField.getText(), 
                        moreInfoTextArea.getText(), null);
            } else {
                Main.db.contact.add(emailTextField.getText(), nameTextField.getText(), 
                        forenameTextField.getText(), phonenumberTextField.getText(), 
                        moreInfoTextArea.getText());
            }
        } catch (Exception e) {
            // TODO remove
            e.printStackTrace();
            return;
        }
}//GEN-LAST:event_okButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewUser().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JLabel forenameLabel;
    private javax.swing.JTextField forenameTextField;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel loginPanel;
    private javax.swing.JLabel loginPassword2Label;
    private javax.swing.JPasswordField loginPasswordField1;
    private javax.swing.JPasswordField loginPasswordField2;
    private javax.swing.JLabel loginPasswordLabel1;
    private javax.swing.JLabel loginUsernameLabel;
    private javax.swing.JTextField loginUsernameTextField;
    private javax.swing.JLabel moreInfoLabel;
    private javax.swing.JTextArea moreInfoTextArea;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel necessaryStarsLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JPanel otherDataPanel;
    private javax.swing.JLabel phonenumberLabel;
    private javax.swing.JTextField phonenumberTextField;
    private javax.swing.JLabel starLabel1;
    private javax.swing.JLabel starLabel2;
    private javax.swing.JLabel starLabel3;
    private javax.swing.JLabel starLabel4;
    // End of variables declaration//GEN-END:variables

}
